{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获取第一主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = np.empty((100, 2))\n",
    "X[:, 0] = np.random.uniform(0., 100, size = 100)\n",
    "X[:, 1] = 0.75 * X[: ,0] + 3. + np.random.normal(0., 10, size = 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def demean(X):\n",
    "    return X - np.mean(X, axis=0)\n",
    "X = demean(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.2647660696529783e-14"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.7449158197850926e-15"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAGnJJREFUeJzt3X+MXWWdx/H3t9MBhiXugIDCtLV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MscqUQ/W6Zne/N3rPLbQe/+9s/1jM+ytzzSnU/fpOYGZnAN8FPuHu\nv211eOvJzK4EfuXu+8zs0vbhmLdW/u+tgN+Hu78j7riZXQSsAZ6I/jGsAB4zs4tp9QZWdrx9BfB8\nzk3NTNI1t5nZ9cCVwNt9qa630tecQt2v7zgzG6cV7O909+9Fh//HzM5z9xei9OSvymth5jYAV5vZ\n5cBpwGto9fgnzWx51Muvxd9bKZ0hufsBdz/X3Ve7+2paAeGt7v5LYCfwoaha5xLg5fbjcNWZ2WXA\nTcDV7n6k41s7gWvN7FQzW0NrwPrfy2hjTn4MrI0qN06hNUC9s+Q2ZS7KXX8TeMrdv9TxrZ3A9dHX\n1wP3Ft22vLj7NndfEf07vhbY7e4fAB4C3hu9rRbXrB5+Pu4HLqc1cHkE+HC5zcnUPwKnAg9ETzZ7\n3P2j7v6kmd0D/IRWqudGdz9WYjsz5e5HzexjwC5gDPgnd3+y5GblYQPwQeCAmT0eHbsZmAXuMbMb\naFWkva+k9hXpJuAuM7sV2E/rRlhpmmkrItIQSumIiDSEAr6ISEMo4IuINIQCvohIQyjgi4g0hAK+\niEhDKOCLiDSEAr6ISEP8PxqDd7mVcLc3AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x111acb198>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:, 0], X[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def f(w, X):\n",
    "    return np.sum((X.dot(w))**2) / len(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def df(w, X):\n",
    "    return X.T.dot(X.dot(w)) * 2/ len(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def direction(w):\n",
    "    return w / np.linalg.norm(w)\n",
    "\n",
    "def first_component(X, initail_w, eta, max_iter = 1e4, epsilon = 1e-8):\n",
    "    w  = direction(initail_w)\n",
    "    cur_iter = 0\n",
    "    \n",
    "    while cur_iter < max_iter:\n",
    "        gradient = df(w, X)\n",
    "        last_w = w\n",
    "        w = w + eta * gradient\n",
    "        w = direction(w)\n",
    "        \n",
    "        if abs(f(last_w, X) - f(w, X)) < epsilon:\n",
    "            break\n",
    "            \n",
    "        cur_iter += 1\n",
    "    \n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = demean(X)\n",
    "initail_w = np.random.random(X.shape[1])\n",
    "eta = 0.001\n",
    "w = first_component(X, initail_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.78842055,  0.6151366 ])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X2 = X - X.dot(w).reshape(-1, 1)*w\n",
    "for i in range(len(X)):\n",
    "    X2[i] = X[i] - X[i].dot(w)*w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAF5BJREFUeJzt3X9s3PV9x/HXG/faHVlVg0goueIl\ni7JspR4+1eKHok2UtTht13JkYwER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Oa/qzRItz9BUlzd+i7VtKj2Z8flZSqaaOqhMAPT0LSG3mPT2WPtapb\nzezF7D+Hm/KfuwFF7XPN55KeNbOjZrap3o2psQvd/U1Jyn5fVuf2LAoboARgZs9J+mSBp+5096eK\nvazAsaYdElXqv4Gkb0m6X9O/3/2SHpD0N7VrXU211Oe6QGvdfdzMlkk6ZGY/z/aG0SQI/ADc/fOL\neNkpSRfnPf6UpPHqtKj2gv43MLOHJX0/5ObUU0t9rgvh7uPZ76fN7ElNl7eiEvhvmdlF7v6mmV0k\n6XS9G7QYlHTC87SkG8zsY2a2UtJqST+pc5tCkf0DkHOdpm9kt6qfSlptZivN7KOavjH/dJ3bFDoz\nW2JmH8/9LOkatfbnPNfTkm7O/nyzpGL/sm9o9PArZGbXSfoXSUslHTCzEXfvcfeTZrZP0s8kfSDp\n6+4+Veq9mtg3zaxL06WN1yR9rb7NCY+7f2Bmt0oakNQm6RF3P1nnZtXChZKeNDNpOje+4+7P1LdJ\n4TCzxyRdJekCMzsl6R5JOyXtM7O/lTQm6fr6tXDxmGkLABFBSQcAIoLAB4CIIPABICIIfACICAIf\nACKCwAeAiCDwASAiCHwAiIj/B9LrPqrwrL+TAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1148cb780>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:, 0], X2[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.61513649,  0.78842064])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2 = first_component(X2, initail_w, eta)\n",
    "w2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.4126242853240711e-07"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w.dot(w2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def first_n_components(n, X, eta = 0.01, max_iter = 1e4, epsilon=1e-8):\n",
    "    X_pca = X.copy()\n",
    "    \n",
    "    X_pca = demean(X_pca)\n",
    "    \n",
    "    res = []\n",
    "    \n",
    "    for i in range(n):\n",
    "        initial_w = np.random.random(X_pca.shape[1])\n",
    "        w = first_component(X_pca, initial_w, eta) \n",
    "        res.append(w)\n",
    "        print(w)\n",
    "        X_pca = X_pca - X_pca.dot(w).reshape(-1, 1) * w\n",
    "        \n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.78842055  0.6151366 ]\n",
      "[ 0.6151366  -0.78842055]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[array([ 0.78842055,  0.6151366 ]), array([ 0.6151366 , -0.78842055])]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_n_components(2, X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from PCA import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(n_components = 2)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca = PCA(2)\n",
    "pca.fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.n_components"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.78842056,  0.61513659],\n",
       "       [ 0.61513672, -0.78842046]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.components_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
